import { useDispatch, useSelector } from 'react-redux'
import { changePlaylist, changePlayingSongIndex, selectAllSongs, selectPlayListType, selectPlayListId } from '../store/reducer/songSlice'
import { IsongDataItem } from '../api/api/selectPageList/type'
/**
 * 
 * @param {
 *   listId: 歌单id,
 *   listType: 歌单类型,
 *   songListData: 歌单数据,
 *   playingId: 正在播放的歌曲id
 * } 
 * param0 
 * @returns 点击播放歌曲的回调函数
 */
interface useChangePlayListProps {
    listId: string,
    listType: string,
    songListData: IsongDataItem[],
    playingId: string
}
function useChangePlayList({
    listId,
    listType,
    songListData,
    playingId
}: useChangePlayListProps) {
    const dispatch = useDispatch()
    const playinglist = useSelector(selectAllSongs);
    const playListId = useSelector(selectPlayListId);
    const playListType = useSelector(selectPlayListType);
    /**
     * 
     * @param 
     * id: 歌曲id,
     */
    const handlePlaySong = (songId: string) => {
        //如果当前歌曲不在歌单内，将当前歌单添加到播放器,或切换歌单
        if (playinglist.findIndex((songInfo) => songInfo.id == songId) == -1 || listId != playListId || listType != playListType) {
            dispatch(changePlaylist({
                playlist: songListData,
                playlistId: listId,
                playListType
            }))
        }
        dispatch(changePlayingSongIndex({
            prePlayingIndex: playingId,
            currentPlayIndex: songId
        }))
    }
    return {
        handlePlaySong,
    }
}
export default useChangePlayList;